﻿package mortal.game.view.copy.defenseCopy
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import extend.language.*;
    import flash.geom.*;
    import mortal.common.*;
    import mortal.common.net.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.common.guide.*;
    import mortal.game.view.common.guide.data.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.GroupTowerCopy.panel.*;
    import mortal.game.view.copy.groupCopy.data.*;
    import mortal.game.view.copy.groupCopy.view.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.pack.*;
    import mortal.game.view.skill.*;
    import mortal.mvc.core.*;

    public class DefenseCopyController extends CopyControllerBase
    {
        private var _navBar:DefenseCopyNavBar;
        private var _miniMapView:DefenseMiniMap;

        public function DefenseCopyController()
        {
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            var arr:Array;
            var copy:* = param1;
            super.enterCopy(copy);
            if (copy.type == ECopyType._ECopyTypeDefense && !Cache.instance.buff.isHasBuffByType(EBuffType._EBuffTypeExp))
            {
                var showItem:* = function (param1:Array) : void
            {
                var _loc_2:* = new GuideUseItemData();
                _loc_2.itemData = param1[0];
                _loc_2.isGuideToUseItem = true;
                _loc_2.isCountdown = false;
                _loc_2.showTxt = "" + Language.getString(70864);
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideShowBackPackItem, _loc_2));
                return;
            }// end function
            ;
                arr = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupProp, EProp._EPropDrug, EDrug._EDrugMoreExp);
                if (arr.length)
                {
                    CallLater.setCallLater(showItem, 1, arr[0]);
                }
                GroupCopyUtil.copyId = copy.code;
            }
            if (copy.type == ECopyType._ECopyTypeDefense && copy.group == 138)
            {
                BuffUtil.addBuffByCode(23000046);
            }
            if (GroupCopyWaitingView.isViewShow)
            {
                GroupCopyWaitingView.instance.hide();
            }
            this.addMiniMap(copy.group);
            this.addNavBar();
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyGroupWinClose));
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            GroupCopyUtil.rqeOneCopyRew(GroupCopyUtil.copyId);
            this.removeDefenseUI();
            this.useExpItem();
            cache.copy.defenseCopy.isCopyEnd = false;
            Cache.instance.copy.defenseCopy.szBlood = null;
            cache.copy.defenseCopy.clear();
            cache.pack.defenseBag.clear();
            cache.pack.defenseRoleEquipCache.clear();
            cache.cd.unregisterAllCDDataByType(CDDataType.skillSeries);
            if (DefenseCopyShopNew.hasInstance && !DefenseCopyShopNew.instance.isHide)
            {
                DefenseCopyShopNew.instance.hide();
            }
            if (DefenseCopyBag.hasInstance && !DefenseCopyBag.instance.isHide)
            {
                DefenseCopyBag.instance.hide();
            }
            if (AutoGuideController._guidingMainId == 33 || AutoGuideController._guidingMainId == 34)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideEndByEvent));
            }
            BuffUtil.removeBuffByCode(23000046);
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyEnd, this.onCopyEnd);
            return;
        }// end function

        override protected function addListeners() : void
        {
            Dispatcher.addEventListener(EventName.DefenseCopyShopBuy, this.onBuyHandler);
            Dispatcher.addEventListener(EventName.DefenseCopyShopSell, this.onSellHandler);
            Dispatcher.addEventListener(EventName.DefenseCopyUseItem, this.onUseHandler);
            Dispatcher.addEventListener(EventName.DefenseCopyTakeEquip, this.onTakeEquipHandler);
            Dispatcher.addEventListener(EventName.DefenseCopyThrowEquip, this.onThrowEquipHandler);
            Dispatcher.addEventListener(EventName.DefenseCopyTidy, this.onTidyHandler);
            Dispatcher.addEventListener(EventName.DefenseCopyUseEquip, this.onUseEquipHandler);
            Dispatcher.addEventListener(EventName.DefenseCopyBagMove, this.onBagMoveHandler);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyBagInit, this.onBagInit);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyBagUpdate, this.onBagUpdte);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyRoleEquipUpdate, this.onEquipUpdate);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyShopUpdate, this.onShopUpdate);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyInfoUpdate, this.onCopyInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyRewardUpdate, this.onCopyInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.DefenseCopyDressSucceed, this.onDressSucceed);
            Dispatcher.addEventListener(EventName.AutoGuideDefenseCopyChooseToSell, this.guideChooseToSellHandler);
            EventUtil.addNetEvent(ServerCommand.DefenseCopyStuffLiftUpdate, this.onBloodUpdate, true);
            return;
        }// end function

        private function guideChooseToSellHandler(event:DataEvent) : void
        {
            var _loc_2:Point = null;
            var _loc_3:AutoGuideData = null;
            if (DefenseCopyBag.hasInstance && !DefenseCopyBag.instance.isHide)
            {
                _loc_2 = DefenseCopyBag.instance.getItemPointByCode(GameConst.GuideDefenceSellCode);
                if (_loc_2 == null)
                {
                    return;
                }
                _loc_3 = AutoGuideConfig.instance.getConfigs(["mainId", "stepId"], [34, 4])[0] as AutoGuideData;
                _loc_3.clickChildName = int(_loc_2.x).toString() + ";" + int(_loc_2.y).toString() + ";45;45";
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideActiveByEvent, {mainId:34, stepId:4}));
            }
            return;
        }// end function

        override protected function removeListeners() : void
        {
            Dispatcher.removeEventListener(EventName.DefenseCopyShopBuy, this.onBuyHandler);
            Dispatcher.removeEventListener(EventName.DefenseCopyShopSell, this.onSellHandler);
            Dispatcher.removeEventListener(EventName.DefenseCopyUseItem, this.onUseHandler);
            Dispatcher.removeEventListener(EventName.DefenseCopyTakeEquip, this.onTakeEquipHandler);
            Dispatcher.removeEventListener(EventName.DefenseCopyThrowEquip, this.onThrowEquipHandler);
            Dispatcher.removeEventListener(EventName.DefenseCopyTidy, this.onTidyHandler);
            Dispatcher.removeEventListener(EventName.DefenseCopyUseEquip, this.onUseEquipHandler);
            Dispatcher.removeEventListener(EventName.DefenseCopyBagMove, this.onBagMoveHandler);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyBagInit, this.onBagInit);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyBagUpdate, this.onBagUpdte);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyRoleEquipUpdate, this.onEquipUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyShopUpdate, this.onShopUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyInfoUpdate, this.onCopyInfoUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.DefenseCopyRewardUpdate, this.onCopyInfoUpdate);
            EventUtil.addNetEvent(ServerCommand.DefenseCopyStuffLiftUpdate, this.onBloodUpdate, false);
            return;
        }// end function

        private function onBloodUpdate(param1:Object) : void
        {
            var _loc_2:SSeqAttributeUpdate = null;
            var _loc_3:SAttributeUpdate = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            if (this._miniMapView)
            {
                _loc_2 = Cache.instance.copy.defenseCopy.szBlood;
                if (_loc_2 && _loc_2.updates.length > 0)
                {
                    _loc_3 = _loc_2.updates[0];
                    _loc_4 = _loc_3.value;
                    _loc_5 = parseInt(_loc_3.valueStr);
                    this._miniMapView.updateLife(_loc_4, _loc_5);
                }
            }
            return;
        }// end function

        private function onBagMoveHandler(event:DataEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_2:* = event.data as DragDropData;
            var _loc_5:* = Cache.instance.pack.getPackChacheByPosType(_loc_2.fromPosType);
            _loc_3 = _loc_5.getIndexByUid(_loc_2.uid);
            _loc_4 = _loc_2.toIndex;
            _loc_2.fromIndex = _loc_3;
            _loc_2.toIndex = _loc_4;
            if (_loc_4 <= _loc_5.sbag.capacity)
            {
                if (_loc_5.changeItemPos(_loc_2))
                {
                    this.onBagUpdte();
                }
            }
            return;
        }// end function

        private function onUseEquipHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data.entityIds;
            var _loc_3:* = event.data.skillId;
            if (_loc_3 > 0 && SkillUtil.isBeidongSkill(_loc_3))
            {
                MsgManager.showRollTipsMsg("" + Language.getString(70865));
                return;
            }
            if (SkillUtil.needSelectTarget(_loc_3))
            {
                if (ThingUtil.selectEntity)
                {
                    _loc_2.push(ThingUtil.selectEntity.entityInfo.entityInfo.entityId);
                }
                else
                {
                    MsgManager.showRollTipsMsg("" + Language.getString(70866));
                    return;
                }
            }
            var _loc_4:* = cache.cd.getCDDataByKeyType(CDDataType.skillSeries, SkillUtil.getSkillSeries(_loc_3).toString());
            if (_loc_4 && _loc_4.isCoolDown)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(70867));
                return;
            }
            if (_loc_3 > 0 && RolePlayer.instance.actionName == ActionName.Tornado)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(70868));
                return;
            }
            GameProxy.sceneProxy.fight(_loc_2, _loc_3, null, 0, "", true);
            return;
        }// end function

        private function onBuyHandler(event:DataEvent) : void
        {
            GameProxy.copy.defenseCopyBuy(event.data.itemCode, event.data.amount);
            return;
        }// end function

        private function onThrowEquipHandler(event:DataEvent) : void
        {
            GameProxy.copy.defenseDiscard(event.data as String);
            return;
        }// end function

        private function onTidyHandler(event:DataEvent) : void
        {
            cache.pack.defenseBag.sortItems();
            this.onBagUpdte();
            return;
        }// end function

        private function onTakeEquipHandler(event:DataEvent) : void
        {
            GameProxy.copy.defenseCopyDress(event.data.putOnUid, event.data.putOffUid, event.data.itemData, event.data.p);
            return;
        }// end function

        private function onUseHandler(event:DataEvent) : void
        {
            GameProxy.copy.defenseUse(event.data.uid, event.data.amount);
            return;
        }// end function

        private function onSellHandler(event:DataEvent) : void
        {
            GameProxy.copy.defenseCopySell(event.data.toString());
            return;
        }// end function

        private function onEquipUpdate(param1:Object) : void
        {
            if (this._navBar == null)
            {
                this.addNavBar();
            }
            this._navBar.updateEquip();
            return;
        }// end function

        private function onCopyInfoUpdate(param1:Object) : void
        {
            if (this._miniMapView == null)
            {
                this.addMiniMap(GroupCopyUtil.copyId);
            }
            if (this._navBar == null)
            {
                this.addNavBar();
            }
            this._miniMapView.updateAll();
            this.onBagUpdte(true);
            return;
        }// end function

        private function onDressSucceed(param1:Object) : void
        {
            var _loc_2:* = param1.p as Point;
            var _loc_3:* = this.navBar.getEquipItemPointByUid(ItemData(param1.itemData).uid);
            if (_loc_3 == null)
            {
                _loc_3 = new Point(965, 629);
            }
            var _loc_4:Array = [];
            _loc_4[0] = param1.itemData;
            _loc_4[1] = _loc_2;
            _loc_4[2] = _loc_3;
            Dispatcher.dispatchEvent(new DataEvent(EventName.FlyItemToAnyPoint, _loc_4));
            return;
        }// end function

        private function onCopyEnd(param1:Object) : void
        {
            var _loc_2:* = param1 as EDefenseCopyResult;
            if (_loc_2.__value == EDefenseCopyResult._EDefenseCopyFailedGoldStuffDead || _loc_2.__value == EDefenseCopyResult._EDefenseCopyFailedTimeOut)
            {
                MsgManager.showTaskTarget(Language.getString(50161));
                this.showPass(_loc_2.__value);
            }
            else if (_loc_2.__value == EDefenseCopyResult._EDefenseCopyFailedLeave)
            {
                this.showPass(_loc_2.__value);
            }
            else if (_loc_2.__value == EDefenseCopyResult._EDefenseCopySuccess)
            {
                MsgManager.showTaskTarget(Language.getString(50160));
                this.showPass(_loc_2.__value);
            }
            return;
        }// end function

        private function showPass(param1:int) : void
        {
            var _loc_2:* = DefenseCopyUtil.getPassInfoArr();
            GroupTowerFaildWin.instance.show();
            GroupTowerFaildWin.instance.updateFailInfo(_loc_2);
            return;
        }// end function

        private function onShopUpdate(param1:Object) : void
        {
            return;
        }// end function

        private function onBagInit(param1:Object = null) : void
        {
            if (this.navBar && !this.navBar.isHide)
            {
                this.navBar.updateEquip();
            }
            return;
        }// end function

        private function onBagUpdte(param1:Object = null) : void
        {
            if (DefenseCopyBag.hasInstance && !DefenseCopyBag.instance.isHide)
            {
                DefenseCopyBag.instance.updateAll(param1);
            }
            if (this.navBar && Cache.instance.pack.defenseBag.itemLength <= 0)
            {
                this.navBar.updateBagNotice(false);
            }
            return;
        }// end function

        public function addMiniMap(param1:int) : void
        {
            if (DefenseCopyUtil.groupDefenseCode == param1)
            {
                DefenseMiniMap.slabelName = ImagesConst.copyInfoPanelTitle_groupCopy;
            }
            else
            {
                DefenseMiniMap.slabelName = ImagesConst.copyInfoPanelTitle_goddesCopy;
            }
            if (this._miniMapView == null)
            {
                this._miniMapView = new DefenseMiniMap();
            }
            this._miniMapView.show(500, 500);
            this._miniMapView.x = Global.stage.stageWidth - 50 - 238;
            this._miniMapView.y = 116;
            return;
        }// end function

        public function removeMiniMap() : void
        {
            if (this._miniMapView)
            {
                this._miniMapView.hide();
                this._miniMapView = null;
            }
            return;
        }// end function

        public function addNavBar() : void
        {
            if (this._navBar == null)
            {
                this._navBar = new DefenseCopyNavBar();
            }
            this._navBar.show();
            return;
        }// end function

        public function removeNavBar() : void
        {
            if (this._navBar)
            {
                this._navBar.hide();
                this._navBar = null;
            }
            return;
        }// end function

        private function removeDefenseUI() : void
        {
            this.removeMiniMap();
            this.removeNavBar();
            if (DefenseCopyBag.hasInstance && !DefenseCopyBag.instance.isHide)
            {
                DefenseCopyBag.instance.hide();
            }
            if (DefenseCopyShop.hasInstance() && !DefenseCopyShop.instance.isHide)
            {
                DefenseCopyShop.instance.hide();
            }
            return;
        }// end function

        private function useExpItem() : void
        {
            var _loc_2:GuideUseItemData = null;
            var _loc_1:* = cache.pack.backPackCache.getFirstItemByCode(110071004);
            if (_loc_1)
            {
                _loc_2 = new GuideUseItemData();
                _loc_2.itemData = _loc_1;
                _loc_2.isCountdown = false;
                _loc_2.isUseMaxNum = true;
                _loc_2.isGuideToUseItem = true;
                _loc_2.showTxt = "" + Language.getString(70869);
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideShowBackPackItem, _loc_2));
            }
            return;
        }// end function

        public function get navBar() : DefenseCopyNavBar
        {
            return this._navBar;
        }// end function

        public function get miniMapView() : DefenseMiniMap
        {
            return this._miniMapView;
        }// end function

    }
}
